package lib_model.Account;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Account_credit_history implements ISql {

	public int id;
	public String reason;
	public int credit_id_ref;
	public int customer_id_ref;

	public Account_credit_history() {
	}

	@Override
	public ReturnObj Select() {
		String sql = "select id, reason, credit_id_ref, customer_id_ref from dbo.account_credit_history";
		ReturnObj obj = new ReturnObj();
		ArrayList<Account_credit_history> arr = new ArrayList<Account_credit_history>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Account_credit_history a = new Account_credit_history();
				a.id = rs.getInt("id");
				a.reason = rs.getString("reason");
				a.credit_id_ref = rs.getInt("credit_id_ref");
				a.customer_id_ref = rs.getInt("customer_id_ref");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.account_credit_history  (id, reason,credit_id_ref, customer_id_ref) values   (?,  ?,  ?,  ?);";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			preState.setString(2, this.reason);
			preState.setInt(3, this.credit_id_ref);
			preState.setInt(4, this.customer_id_ref);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.account_credit_history set reason = ?,credit_id_ref = ?,customer_id_ref = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.reason);
			preState.setInt(2, this.credit_id_ref);
			preState.setInt(3, this.customer_id_ref);
			preState.setInt(4, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.account_credit_history where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id,reason, credit_id_ref, customer_id_ref from dbo.account_credit_history where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Account_credit_history a = new Account_credit_history();
				a.id = rs.getInt("id");
				a.reason = rs.getString("reason");
				a.credit_id_ref = rs.getInt("credit_id_ref");
				a.customer_id_ref = rs.getInt("customer_id_ref");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	public Account_credit_history(int id) {
		super();
		this.id = id;
	}

	
}
